package com.syni.mdd.sjb.common.datatreaure.listen.order;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppServiceOrder;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppServices;
import com.syni.mdd.sjb.common.component.utils.common.utils.SpringUtils;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mdd.sjb.common.dxshapp.CommonPushToDsxhAppDto;
import com.syni.mdd.sjb.common.datatreaure.constants.Constants;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.dataBankBusiness.NewDetailParamsRpVo;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppNewsService;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppServiceOrderService;
import lombok.extern.log4j.Log4j2;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.HashMap;
import java.util.Map;

@Log4j2
public class DataBankOrderListenerHandler implements Job {
    @Override
    public synchronized void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        synchronized (DataBankOrderListenerHandler.class) {
            System.out.println("DataBankOrderListenerHandler info start >>>>>>>>>");
            DxshAppNewsService dxshAppNewsService = SpringUtils.getBean(DxshAppNewsService.class);
            DxshAppServiceOrderService dxshAppServiceOrderService = SpringUtils.getBean(DxshAppServiceOrderService.class);
            Map<String, Object> paramsMap = jobExecutionContext.getJobDetail().getJobDataMap();
            DxshAppServices dxshAppServices = (DxshAppServices) paramsMap.get("dxshAppServices");
            Integer dxshAppServiceOrderId = Integer.parseInt(paramsMap.get("dxshAppServiceOrderId").toString());
            DxshAppServiceOrder dxshAppServiceOrder = dxshAppServiceOrderService.getById(dxshAppServiceOrderId);
            if (dxshAppServiceOrder == null) {
                return;
            }
            String suffix = paramsMap.get("suffix").toString();
            //构建一个推送对象
            CommonPushToDsxhAppDto commonPushToDsxhAppDto = new CommonPushToDsxhAppDto();
            commonPushToDsxhAppDto.setCode(String.valueOf(NewDetailParamsRpVo.ORTHER_MES_SERVICE_PUR_RE));
            commonPushToDsxhAppDto.setType(Constants.APP_NEWS_SYSTEM);
            commonPushToDsxhAppDto.setCode(String.valueOf(NewDetailParamsRpVo.ORTHER_MES_SERVICE_PUR_RE));
            commonPushToDsxhAppDto.setAlert(String.format("您的【%s】服务还没付款哦，请给我们一个机会为您服务！", dxshAppServices.getTitle()));
            commonPushToDsxhAppDto.setUserId(dxshAppServiceOrder.getBmsUserId());
            commonPushToDsxhAppDto.setDescribes("服务购买待付款提醒");
            commonPushToDsxhAppDto.setMainTitle("系统消息");
            commonPushToDsxhAppDto.setMessageType(Constants.DANK_OTHER_INFORM);
            commonPushToDsxhAppDto.setMessageSecondType(Constants.DANK_OTHER_INFORM_SYS_NOTIFICATION);
            Map<String, Object> content = new HashMap<>();
            content.put("newType", "servicePurchaseRemind");
            content.put("serviceId", dxshAppServices.getId());
            commonPushToDsxhAppDto.setContent(JSON.toJSONString(content));
            dxshAppNewsService.commonPushToDsxhApp_v2(commonPushToDsxhAppDto);
            if (dxshAppServiceOrder != null) {
                if (suffix.equals("_1")) {
                    dxshAppServiceOrder.setFirstInfoStatus(Constants.COMMON_FLAG_TRUE_INT);
                } else if (suffix.equals("_2")) {
                    dxshAppServiceOrder.setSecondInfoStatus(Constants.COMMON_FLAG_TRUE_INT);
                }
                log.info("dxshAppServiceOrder :{}", JSON.toJSONString(dxshAppServiceOrder));
                dxshAppServiceOrderService.updateById(dxshAppServiceOrder);
            }
        }
    }
}
